<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>登录</title>
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/login.css">
    <!-- 可选的Bootstrap主题文件（一般不用引入） -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
    <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    <script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="${pageContext.request.contextPath}/js/myjs.js"></script>
</head>
<body>
<div class="login-box">
    <form method="post" id="login-form">
        <input type="hidden" name="uuid" id="uuid" value="<s:property value='@java.util.UUID@randomUUID()'/>"/>
        <a href="${pageContext.request.contextPath}/jsp/front/register.jsp">注册</a>

        <div class="form-group has-feedback">
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                <input type="text" name="account" id="account" class="form-control" placeholder="邮箱/用户名"/>
            </div>
        </div>
        <div class="form-group has-feedback" id="pwddiv">
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-briefcase"></span></span>
                <input type="password" name="password" id="pwd" class="form-control" placeholder="密码"/>
            </div>
        </div>
        <div class="form-group has-feedback" id="codeMsg" style="display: none">
            <div class="input-group">
                <input type="text" name="code" class="form-control" placeholder="验证码" style="width:100px;"/>
                <a href="javascrip:;" style="margin-left:20px;">
                    <img alt="验证码" title="点击更换" width="75" height="30" id="imageCode"/>
                </a>
            </div>
        </div>

        <div>
            <label>
                <input type="checkbox" name="remenber" value="yes">
                记住密码
            </label>
        </div>
        <div class="login-btn">
            <button type="button" class="btn btn-success" id="btn" >登&nbsp;&nbsp;&nbsp;&nbsp;录</button>
        </div>
        <span id="msg" style="color:red"></span>
    </form>
</div>
</body>
</html>
<script>
    $(function () {
        $("#login-form input").focus(function(){
            $(this).parent().parent().removeClass("has-error");
            $(this).parent().parent().addClass("has-warning");
        });

        $("#login-form input").blur(function(){
            $(this).parent().parent().removeClass("has-error");
            $(this).parent().parent().removeClass("has-warning");
            $(this).parent().parent().removeClass("has-success");
        });

        $("#login-form input").on('keyup', function(event) {
            if (event.keyCode == '13') {
                login_fun();
            }
        });

        $("#account").blur(function () {
            $.ajax({
                url: "${pageContext.request.contextPath}/login/loginAction!ipAddress",
                data: {"account": $("#account").val()},
                dataType: "json",
                success: function (data) {
                    if (data.showCode) {
                        $("#codeMsg").show();
                    }
                }
            })
        });

        $("#btn").click(function () {
            login_fun();
        });

        $("#imageCode").attr("src", "${pageContext.request.contextPath}/codeAction!imageCode.action?uuid=" + $('#uuid').val());
        $('#imageCode').click(function () {
            $(this).attr("src", "${pageContext.request.contextPath}/codeAction!imageCode.action?r=" + Math.random() + "&uuid=" + $('#uuid').val());
            return false;
        });
    });

    function addCookie(name, value, expiresHours) {
        var cookieString = name + "=" + value;
        //判断是否设置过期时间
        if (expiresHours > 0) {
            var date = new Date();
            date.setTime(date.getTime() + expiresHours*24*3600*1000);
            cookieString = cookieString + "; path=/; expires=" + date.toGMTString();
        }
        document.cookie = cookieString;
    }

    function login_fun(){
        $.ajax({
            type: "post",
            url: "${pageContext.request.contextPath}/login/loginAction!login.do",
            data: $("form").serialize(),
            success: function (data) {
                var obj = $.parseJSON(data);
                if (!obj.success) {
                    $("#imageCode").attr("src", "${pageContext.request.contextPath}/codeAction!imageCode.action?r=" + Math.random() + "&uuid=" + $('#uuid').val());
                    if(obj.msg == '验证码输入错误!'){
                        $("input[name=code]").parent().parent().addClass("has-error");
                    }else {
                        $("#pwddiv").addClass("has-error");
                    }
                    $("#msg").html(obj.msg);
                } else {
                    if (obj.cokrem == 'yes') {
                        addCookie(obj.cokiName,obj.cokvalue,obj.cokMaxAge);
                    }

                    if(obj.curAcount == "system" || obj.curAcount == "admin"){
                        window.location.href = "${pageContext.request.contextPath}/jsp/backend/back_index.jsp";
                    }else{
                        window.location.href = "${pageContext.request.contextPath}/jsp/index.jsp";
                    }

                }
                if (obj.showCode) {
                    $("#codeMsg").show();
                }
            }
        });
    }
</script>